package com.lottery.im.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.lottery.im.R;
import com.lottery.im.util.BackHandlerHelper;
import com.lottery.im.util.FragmentBackHandler;
import com.lottery.im.util.ToastUtils;
import com.lottery.im.widget.dialog.LoadingDialogUtil;
import com.trello.rxlifecycle2.components.support.RxFragment;

/**
 * * 基础 Fragment 类，提供各种基础功能。
 */

public abstract class BaseFragment<T extends BaseFragmentPresenter> extends RxFragment
    implements FragmentBackHandler {

  protected T presenter;
  protected Context mContext;
  protected Activity mActivity;
  protected Fragment mFragment;
  protected Unbinder unbinder;

  /**
   * 显示键盘
   */
  protected void showKeyBoard() {
    InputMethodManager imm = (InputMethodManager) getActivity().getApplicationContext()
        .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(0, InputMethod.SHOW_FORCED);
  }

  /**
   * 隐藏键盘
   */
  protected boolean hideKeyBoard() {
    //		return mBaseActivityImpl.hideKeyBoard();
    final InputMethodManager imm = (InputMethodManager) getActivity().getApplicationContext()
        .getSystemService(Context.INPUT_METHOD_SERVICE);
    return imm.hideSoftInputFromWindow(getActivity().findViewById(android.R.id.content)
        .getWindowToken(), 0);
  }

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = getActivity();
    mContext = getActivity().getApplicationContext();
    mFragment = this;
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(getLayoutResId(), container, false);
    unbinder = ButterKnife.bind(this, view);
    initPresenter();
    return view;
  }

  protected abstract int getLayoutResId();

  protected abstract void initPresenter();

  @Override
  public final boolean onBackPressed() {
    return interceptBackPressed()
        || (getBackHandleViewPager() == null
        ? BackHandlerHelper.handleBackPress(this)
        : BackHandlerHelper.handleBackPress(getBackHandleViewPager()));
  }

  public boolean interceptBackPressed() {
    return false;
  }

  /**
   * 2.1 版本已经不在需要单独对ViewPager处理
   *
   * @deprecated
   */
  @Deprecated
  public ViewPager getBackHandleViewPager() {
    return null;
  }

  public void showError() {
    ToastUtils.showToast(getActivity(), R.string.error_message);
  }

  public void showToast(String msg) {
    ToastUtils.showToast(getActivity(), msg);
  }

  public void showToast(@StringRes int res) {
    ToastUtils.showToast(getActivity(), mActivity.getResources().getString(res));
  }

  public void showLoadingDialog() {
    LoadingDialogUtil.showProgressDialog(getActivity());
  }

  public void cancelLoadingDialog() {
    LoadingDialogUtil.cancelProgressDialog();
  }

  @Override
  public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
  }
}
